package com.jsjosem.aftersales.ui.event;

import static com.jsjosem.aftersales.constant.Command.REFRESH_EVENT_LIST;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.jsjosem.aftersales.R;
import com.jsjosem.aftersales.adapter.StateAdapter;
import com.jsjosem.aftersales.listener.ListenerManager;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.collection.ListUtil;

public class EventActivity extends AppCompatActivity {
    private TabLayout tabLay;
    private ImageView backIv;
    private List<String> tabList;
    private StateAdapter stateAdapter;
    private ViewPager2 viewPager2;
    private Spinner selectSp;
    private ListenerManager listenerManager;
    private String[] selectList = {"全部事件", "我创建", "我负责", "我协办"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event);
        initView();
        initTab();

        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this, R.layout.item_select, selectList);
        selectSp.setAdapter(arrayAdapter);
        selectSp.setSelection(0, true);
        selectSp.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                Log.d("select", selectList[i]);
                listenerManager.sendBroadCast(REFRESH_EVENT_LIST, selectList[i], "ok");
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        backIv.setOnClickListener(v -> {
            finish();
        });
    }

    private void initView() {
        tabLay = findViewById(R.id.tab_lay);
        viewPager2 = findViewById(R.id.view_page);
        backIv = findViewById(R.id.event_img_back);
        selectSp = findViewById(R.id.sp_select);
        listenerManager = ListenerManager.getInstance();
    }

    private void initTab() {
        tabList = ListUtil.of("待分配", "已分配", "已完成");
        List<Fragment> fragments = new ArrayList<>();
        tabList.forEach(state -> {
            fragments.add(EventListFragment.newInstance(state));
        });
        stateAdapter = new StateAdapter(this, fragments);
        viewPager2.setAdapter(stateAdapter);
        tabLay.setTabMode(TabLayout.MODE_FIXED);
        new TabLayoutMediator(tabLay, viewPager2, (tab, position) -> {
            String state = tabList.get(position);
            tab.setText(state);
        }).attach();

        tabLay.setTabTextColors(Color.parseColor("#666666"), Color.parseColor("#101010"));
        tabLay.setBackground(new ColorDrawable(Color.parseColor("#ffffff")));
    }
}